package com.leaf.lowcode.utils.jsontobean;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.lang.reflect.Field;
import java.util.Iterator;

public class JsonToBeanV<T> {
    public T tansfrom(T cls, JSONArray arry){
        JSONArray jsonArray=arry;
        for(int i=0;i<jsonArray.size();i++)
        {
           // System.out.println("json"+jsonArray);
            JSONObject json = (JSONObject) jsonArray.get(i);
           // System.out.println("json"+json);
            Field[] fields =cls.getClass().getDeclaredFields();
            for(Field field:fields)
            {
                Iterator<String> iterator = json.keySet().iterator();
                while (iterator.hasNext())
                {
                    String name = iterator.next();
                    if (field.getName().equals(name)) {
                        // System.out.println(name);
                        Object o = json.get(name);
                        ToTranfer toTranfer =null;
                      //  System.out.println("field.getType():"+field.getType().toString());
                        String valueName =field.getType().toString();
                        switch (valueName) {
                            case "class java.lang.String":
                                //System.out.println("String");
                                toTranfer=new StringToTransfer();
                                cls= (T) toTranfer.laod(field,o,cls);
                                //System.out.println("cls===="+cls.toString());
                                break;
                            case "double":
                                toTranfer=new DoulbeToTransfer();
                                cls= (T) toTranfer.laod(field,o,cls);
                                break;
                            case "class com.leaf.lowcode.entity.Origin":
                                toTranfer=new OriginToTransfer();
                                cls= (T) toTranfer.laod(field,o,cls);
                                break;
                        }
                    }
                }
            }
        }
        return cls;
    }
}
